package com.sdy.auth.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import com.sdy.auth.biz.mapper.SysRoleMapper;
import com.sdy.auth.biz.model.SysRole;
import com.sdy.auth.biz.model.SysUserRole;
import com.sdy.auth.biz.service.SysRoleService;
import com.sdy.auth.biz.service.SysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2019-06-14
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public void addUsers(List<Integer> userIdList, Integer roleId) {
        Date now = new Date();
        for (Integer userId : userIdList) {
            SysUserRole sysUserRole = new SysUserRole()
                    .setRoleId(roleId)
                    .setUserId(userId)
                    .setCreateTime(now);
            sysUserRoleService.save(sysUserRole);
        }
    }

    @Override
    public void removeUsers(List<Integer> userIdList, Integer roleId) {
        if (userIdList.isEmpty()) {
            return;
        }
        sysUserRoleService.remove(new QueryWrapper<SysUserRole>()
                .eq("role_id", roleId)
                .in("user_id", userIdList)
        );
    }
}
